<template>
  <div class="pay" v-if="order">
    <van-nav-bar title="支付页" fixed />
    <div class="address">
      <div class="left-icon">
        <van-icon name="location-o" />
      </div>
      <div class="content">
        <div class="name">请选择收货地址</div>
        <div class="phone"></div>
        <div class="address-text"></div>
      </div>
      <div class="right-icon">
        <van-icon name="arrow" />
      </div>
    </div>

    <div class="goods-list"  v-if="Redirect">
      <div class="goods-item" v-for="item in list" :key="item.id">
        <div class="left">
          <img :src="item.image" alt="">
        </div>
        <div class="right">
          <div class="name">{{ item.name }}</div>
          <div class="price">¥{{ item.price }}</div>
          <div class="count">x{{ item.num }}</div>
        </div>
      </div>
    </div>
     <div class="goods-list"  v-else>
      <div class="goods-item" v-for="item in Paycartlist" :key="item.id">
        <div class="left">
          <img :src="item.image" alt="">
        </div>
        <div class="right">
          <div class="name">{{ item.name }}</div>
          <div class="price">¥{{ item.price }}</div>
          <div class="count">x{{ item.num }}</div>
        </div>
      </div>
    </div>

    <div class="total-price">
      <span>总计：</span>
      <span class="money">￥{{ totleprice }}</span>
    </div>

    <div class="pay-detail">
      <div class="pay-cell">
        <span>订单总金额：</span>
        <span class="red">￥{{ totleprice }}</span>
      </div>

      <div class="pay-cell">
        <span>优惠券：</span>
        <span>无优惠券可用</span>
      </div>

      <div class="pay-cell">
        <span>配送费用：</span>
        <span v-if="false">请先选择配送地址</span>
        <span v-else class="red">+￥0.00</span>
      </div>
    </div>

    <!-- 支付方式 -->
    <div class="pay-way">
      <span class="tit">支付方式</span>
      <div class="pay-cell">
        <span><van-icon name="balance-o" />余额支付（可用 ¥{{ money }} 元）</span>
        <span class="red"><van-icon name="passed" /></span>
      </div>
    </div>

    <!-- 买家留言 -->
    <div class="buytips">
      <textarea placeholder="选填:买家留言(50字内)" name="" id="" cols="30" rows="10" v-model="title"></textarea>
    </div>

    <!-- 底部提交 -->
    <div class="footer-fixed">
      <div class="left">实付款：<span>￥{{ totleprice }}</span></div>
      <div class="tipsbtn" @click="submit">提交订单</div>
    </div>
  </div>
   <div class="pay" v-else>
    <h1>
      暂无商品
    </h1>
   </div>
</template>

<script>
export default {
  name: 'PayIndex',
  data () {
    return {
      Paycartlist: [],
      list:[],
      money: 0,
      cartlist: [],
      Awaitcartlist: [],
      mode:'cart',
      Redirect:true,
      title:'',
      userInfo:{},
      order:false
    }
  },
  created() {
    this.Redirect = this.redirect || false
    if(this.Redirect){
      this.mode = 'buyNow'
    }
    this.money=localStorage.getItem('money')
    this.getCartList() 
    this.getList()
    this.getOrder()
  },
  computed: {
    totleprice(){
      if(this.Redirect){
        return this.list.reduce((sum, item) => {
        return sum + item.price * item.num 
      }, 0)
      }
      else{
      return this.Paycartlist.reduce((sum, item) => {
        return sum + item.price * item.num 
      }, 0)
    }
    },
    redirect(){
      return this.$route.query.redirect
    },
    updateTotal(){
      return this.Paycartlist.reduce((sum, item) => {
        return sum + item.num
      }, 0)  
    },

  },
  methods: {
    getCartList(){
      this.Paycartlist = JSON.parse(localStorage.getItem('Paycartlist')) || []
      this.Awaitcartlist = JSON.parse(localStorage.getItem('Awaitcartlist')) || []
      this.cartlist = JSON.parse(localStorage.getItem('cartlist')) || []
      this.userInfo = JSON.parse(localStorage.getItem('userInfo')) || {}
    },
    getOrder(){
       if(this.Paycartlist.length>0){
        this.order=true 
       }
       if(this.list.length>0){
        this.order=true
       }
    },
    getList(){
      this.list = JSON.parse(localStorage.getItem('buyNowPaylist')) || []
    },
    submit() {
      if (this.totleprice > this.money) {
        this.$toast('余额不足')
        return 
      }
      if(this.Redirect){
        this.money = this.money - this.totleprice
        this.list.forEach(item => {
          item.date=new Date().toLocaleString()
          item.timestamp= Date.now()
          item.status = '待发货'
        })
        localStorage.setItem('buyNowlist', JSON.stringify(this.list))
         console.log(this.list);
        localStorage.setItem('money',this.money)
        this.$toast('订单提交成功')
        localStorage.removeItem('buyNowPaylist')
        this.$router.push({ path: '/myorder',
        query: {
          dataType: 'buyNow'
           }
       })
      }
      else{
        this.money = this.money - this.totleprice
        this.Paycartlist.forEach(item => {
          item.date=new Date().toLocaleString()
          item.timestamp= Date.now()
          item.status = '待发货'
        })
        this.cartlist=this.cartlist.concat(this.Paycartlist)
        localStorage.setItem('cartlist', JSON.stringify(this.cartlist))
        localStorage.removeItem('Paycartlist')
        this.Awaitcartlist=this.Awaitcartlist.filter(item1=>!this.Paycartlist.some(item2=>item1.id===item2.id))
        localStorage.setItem('Awaitcartlist', JSON.stringify(this.Awaitcartlist))
        localStorage.setItem('money',this.money)      
        this.$toast('订单提交成功')
        this.$router.push({ path: '/myorder',
        query: {
          dataType: 'cart'
           }
       })
      }
    }
  }
}
</script>

<style lang="less" scoped>
.pay {
  padding-top: 46px;
  padding-bottom: 46px;
  ::v-deep {
    .van-nav-bar__arrow {
      color: #333;
    }
  }
}
.address {
  display: flex;
  align-items: center;
  justify-content: flex-start;
  padding: 20px;
  font-size: 14px;
  color: #666;
  position: relative;
  background: url(@/assets/border-line.png) bottom repeat-x;
  background-size: 60px auto;
  .left-icon {
    margin-right: 20px;
  }
  .right-icon {
    position: absolute;
    right: 20px;
    top: 50%;
    transform: translateY(-7px);
  }
}
.goods-item {
  height: 100px;
  margin-bottom: 6px;
  padding: 10px;
  background-color: #fff;
  display: flex;
  .left {
    width: 100px;
    img {
      display: block;
      width: 80px;
      margin: 10px auto;
    }
  }
  .right {
    flex: 1;
    padding: 10px;
    .name {
      font-size: 16px;
      line-height: 24px;
    }
    .price {
      color: #fa2209;
      font-size: 14px;
      line-height: 24px;
    }
    .count {
      color: #959595;
      font-size: 14px;
      line-height: 24px;
    }
  }
}
.total-price {
  padding: 20px;
  font-size: 16px;
  display: flex;
  justify-content: space-between;
  .money {
    color: #fa2209;
  }
}
.pay-detail {
  padding: 20px;
  background-color: #fff;
  .pay-cell {
    display: flex;
    justify-content: space-between;
    line-height: 40px;
    font-size: 14px;
    .red {
      color: #fa2209;
    }
  }
}
.pay-way {
  padding: 20px;
  background-color: #fff;
  margin-top: 10px;
  .tit {
    font-size: 16px;
    display: block;
    margin-bottom: 10px;
  }
  .pay-cell {
    display: flex;
    justify-content: space-between;
    line-height: 40px;
    font-size: 14px;
    .red {
      color: #fa2209;
    }
  }
}
.buytips {
  padding: 20px;
  background-color: #fff;
  margin-top: 10px;
  textarea {
    width: 100%;
    height: 100px;
    border: 1px solid #eee;
    padding: 10px;
    font-size: 14px;
  }
}
.footer-fixed {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
  height: 46px;
  background-color: #fff;
  border-top: 1px solid #eee;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
  .left {
    font-size: 14px;
  }
  .tipsbtn {
    width: 120px;
    height: 36px;
    line-height: 36px;
    background-color: #fa2209;
    color: #fff;
    text-align: center;
    border-radius: 18px;
  }
}
</style>